package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.domain.Customer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.qo.CustomerQueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ICustomerService;
import cn.wolfcode.ssm.service.IEmployeeService;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo")CustomerQueryObject qo) {
        //查询条件为潜在客户
       qo.setStatus(Customer.STATUS_COMMON);
       //得到当前登录的用户
       Subject subject = SecurityUtils.getSubject();
       //如果当前登录用户不是admin或Market_Manager
       if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
           //获取当前登录用户
           Employee employee = (Employee) subject.getPrincipal();
           //将当前用户id设置为查询对象的销售员id,这样用户登录进去就只能看到自己的客户
           qo.setSellerId(employee.getId());
       }
        //根据角色编码查询拥有该角色 Market Market_Manager的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);
        PageInfo<Customer> pageInfo = customerService.selctForPageResult(qo);
        model.addAttribute("pageInfo", pageInfo);
        //根据目录sn查询出明细的title
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentsn("job");
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentsn("source");
        model.addAttribute("jobs",jobs);
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByParentsn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/potentialList";
    }
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo")CustomerQueryObject qo) {
        //查询条件为潜在客户
       qo.setStatus(Customer.STATUS_POOL);
       //得到当前登录的用户
       Subject subject = SecurityUtils.getSubject();
       //如果当前登录用户不是admin或Market_Manager
       if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
           //获取当前登录用户id
           Employee employee = (Employee) subject.getPrincipal();
           //根据当前员工id查询所跟进的客户
           qo.setSellerId(employee.getId());
       }
        //根据角色编码查询拥有该角色的员工 Market Market_Manager
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);
        PageInfo<Customer> pageInfo = customerService.selctForPageResult(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customer/pool";
    }



    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
            if (id != null) {
                customerService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
            if (customer.getId() != null) {
                customerService.update(customer);
            } else {
                customerService.save(customer);
            }
            return new JsonResult();
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id,Integer status){
        customerService.changeStatus(id,status);
        return new JsonResult();
    }

    @RequestMapping("/checkTel")
    @ResponseBody
    public Map<String,Boolean> checkTel(String tel, Long id){  // valid:true
        HashMap<String, Boolean> map = new HashMap<>();
        System.out.println(tel);
        System.out.println(id);
        if(id ==null){
            Customer customer = customerService.selectByTel(tel);
            map.put("valid",customer==null);
            return map;
        }
        Customer one = customerService.getOne(id);
        if(one.getTel().equals(tel)){
            map.put("valid",true);
        }else{
            Customer customer = customerService.selectByTel(tel);
            map.put("valid",customer==null);
        }
        return map;
    }

}
